/**********************************************************************
Compile : gcc -O3 -o extend extend.c ../generation/check.c
Usage : ./extend mode mode1 mode2 N L filename
mode  : 0 normal ; 1 cycled ; 2 triggled cycled
mode1 : 0 disallow self-activation; 1 allow self-activation
mode2 : Additional Requirements: 0 no requirements; 1 restriction I; 2 restriction II; 3 restriction III
N     : number of the nodes                
L     : number of the steps        
Output: All possible candidate processes for cycle
**********************************************************************/

#include <stdio.h>
#include <stdlib.h>

void print_help ()
{
	printf ("Usage         : ./extend mode mode1 mode2 N L filename\n");
	printf ("                (./extend 0 0 3 6 7 N-6x7)\n");
	printf ("mode          : 0 normal ; 1 cycled ; 2 triggled cycled\n");
	printf ("mode1         : 0 disallow self-activation ; 1 allow self_activation\n");
	printf ("mode2         : 0 no Restrictions ; 1 Restriction I ; 2 Restriction II; 3 Restriction I and II\n");
	printf ("N             : number of the nodes\n");
	printf ("L             : number of the steps\n");
	printf ("filename      : input feasible processes\n");
	printf ("Output        : All possible candidate processes for cycle\n");
}

void extend (int N, int L, int pathway[L][N], int i, int mode, int mode1, int mode2)
{
	if (i == N)
	{
		int j, k, flag;

		flag = network_inferred (N, L, pathway, mode, mode1, mode2);
		if (flag == 1)
		{
			printf ("c Candidate CellCycle Pathway\n");

			for (j = 0; j < L; j++)
			{
				for (k = 0; k < N; k++)
				{
					printf ("%d ", pathway[j][k]);
				}
				printf ("\n");
			}
		}
	}
	else
	{
		if ((mode2 == 1 || mode2 == 3) && pathway[1][i] != pathway[2][i])
		{
			pathway[0][i] = pathway[1][i];
			extend (N, L, pathway, i + 1, mode, mode1, mode2);
		}
		else
		{
			pathway[0][i] = 0;
			extend (N, L, pathway, i + 1, mode, mode1, mode2);
			pathway[0][i] = 1;
			extend (N, L, pathway, i + 1, mode, mode1, mode2);
		}
	}
}

int main (int argc, char *argv[])
{
	FILE *fp;
	char buf[256];
	int mode, mode1, mode2, N, L;

	if (argc == 1)
	{
		print_help ();
		exit (0);
	}

	mode  = atoi (argv[1]);
	mode1 = atoi (argv[2]);
	mode2 = atoi (argv[3]);
	N     = atoi (argv[4]);
	L     = atoi (argv[5]);

	int i, j;
	int pathway[L + 1][N];

	fp = fopen (argv[6], "r");
	while (fgets (buf, 256, fp))
	{
		for (i = 0; i < L; i++)
		{
			for (j = 0; j < N; j++)
			{
				fscanf (fp, "%d", &pathway[i + 1][j]);
			}
			fgets (buf, 256, fp);
		}

		extend (N, L + 1, pathway, 0, mode, mode1, mode2);
	}
	fclose (fp);
}
